/*
 * 题库
 */

import { createAPI } from '@/utils/request'

// 基础题库列表
export const list = data => createAPI('/questions', 'get', data)
  .then(res => ({
    ...res.data,
    items: res.data.items.map(d => ({
      ...d,
      difficulty: d.difficulty === '1' ? '简单' : d.difficulty === '2' ? '一般' : '困难',
      questionType: d.questionType === '1' ? '单选' : d.difficulty === '2' ? '多选' : '简答'
    }))
  }))
  .catch(() => {})

// 基础题库详情
export const detail = data => createAPI(`/questions/${data.id}`, 'get', data)
  .then(res => ({
    ...res.data,
    items: res.data.items.map(d => ({
      ...d,
      questionType: d.questionType === '1' ? '单选题' : d.questionType === '2' ? '多选题' : '单选题',
      difficulty: d.difficulty === '1' ? '简单' : d.difficulty === '2' ? '困难' : '复杂'
    }))
  }))
  .catch(() => {})
// 批量导入题
export const batch = data => createAPI('/questions/batch', 'post', data)

// 基础题库添加
export const add = data => createAPI('/questions', 'post', data)

// 基础题库修改
export const update = data => createAPI(`/questions/${data.id}`, 'put', data)

// 基础题库删除
export const remove = data => createAPI(`/questions/${data.id}`, 'delete', data)

// 精选题库加入取消
export const choiceAdd = data =>
  createAPI(`/questions/choice/${data.id}/${data.choiceState}`, 'patch', data)

// 精选题库列表
export const choice = data => createAPI('/questions/choice', 'get', data)
  .then(res => ({
    ...res.data,
    items: res.data.items.map(d => ({
      ...d,
      difficulty: d.difficulty === '1' ? '简单' : d.difficulty === '2' ? '一般' : '困难',
      questionType: d.questionType === '1' ? '单选' : d.questionType === '2' ? '多选' : '简答',
      chkState: d.chkState === 0 ? '待审核' : d.chkState === 1 ? '通过' : '拒绝',
      publishState: d.publishState === 0 ? '待发布' : '已下架'
    }))
  }))
  .catch(() => {})
  // .then(res => res.data)
  // .catch(() => [])

// 精选题库上下架
export const choicePublish = data =>
  createAPI(`/questions/choice/${data.id}/${data.publishState}`, 'post', data)

// 试题审核
export const choiceCheck = data =>
  createAPI(`/questions/check/${data.id}`, 'post', data)

// 试题审核意见列表
export const auditOpinions = data =>
  createAPI(`/questions/${data.id}/auditOpinions`, 'get', data)

// 出题记录
export const setRecords = data =>
  createAPI(`/questions/${data.id}/setRecords`, 'get', data)

// 组题列表
export const randoms = data => createAPI('/questions/randoms', 'get', data)
  .then(res => ({
    ...res.data,
    items: res.data.items.map(d => ({
      ...d,
      questionIDs: d.questionIDs.map(i => i.number).join('\n')
    }))
  }))
  .catch(() => {})

// 组题列表删除
export const removeRandoms = data =>
  createAPI(`/questions/randoms/${data.id}`, 'delete', data)
